package selef.xiafan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import selef.xiafan.entity.system.Stock;
import selef.xiafan.query.StockQuery;
import selef.xiafan.service.IStockService;
import selef.xiafan.util.result.PageResult;
import selef.xiafan.util.result.Result;
import selef.xiafan.util.result.ResultFactory;

/**
 * @author xiafan
 * @version 1.0
 * @date 2021/7/21 23:14
 */
@RestController
@RequestMapping("/api/content/stock")
public class StockContoller {
    @Autowired
    private IStockService iStockService;

    @PostMapping("/listStock")
    public PageResult<Stock> listStock(@RequestBody StockQuery stockQuery){
        return iStockService.listStock(stockQuery);
    }

    @PostMapping("/add_update")
    public Result addOrUpdate(@RequestBody Stock stock){
        Integer result = iStockService.addOrEdit(stock);
        if(result==1){
            return ResultFactory.buildSuccessResult("添加成功");
        }
        if(result==2){
            return ResultFactory.buildSuccessResult("修改成功");
        }
        return ResultFactory.buildFailResult("异常");
    }

    @PutMapping("/changeEnable")
    public Result changeEnable(@RequestBody Stock stock){
        Integer result = iStockService.changeEnable(stock);
        if(result==1){
            return ResultFactory.buildSuccessResult(stock.getName()+"下架成功");
        }
        if(result==2){
            return ResultFactory.buildSuccessResult(stock.getName()+"上架成功");
        }
        return ResultFactory.buildFailResult("异常");
    }

    @PostMapping("/delete")
    public Result deleteStock(@RequestBody Stock stock){
        iStockService.deleteByStock(stock);
        return ResultFactory.buildSuccessResult("删除成功");
    }

    @GetMapping("/stockList")
    public Result stockList(){
        return ResultFactory.buildSuccessResult(iStockService.enabledStockList());
    }
}
